//
//  AppTabBarViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/8.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import QMUIKit
import RxSwift

class AppTabBarViewController: QMUITabBarViewController {
    
    var callItem:ArticleItem?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupVc()
        self.tabBar.isHidden  = false

        // Do any additional setup after loading the view.
    }
    
    func setupVc() {
       
        
        let homeVc = HomeViewController()
        homeVc.view.backgroundColor = UIColor.white
        homeVc.hidesBottomBarWhenPushed = false
        
        let homeAppVc = AppNavViewController(rootViewController: homeVc)
        
        
       
        homeAppVc.tabBarItem.title =  LanguageHelper.getString(key: "TabHome")
        homeAppVc.tabBarItem.image = #imageLiteral(resourceName: "icon_home")
        homeAppVc.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_home_sel")
        homeAppVc.hidesBottomBarWhenPushed = false
        
        let exchange = ExchangeViewController()
        exchange.view.backgroundColor = UIColor.white
        exchange.hidesBottomBarWhenPushed = false
        let exchangeAppVc = AppNavViewController(rootViewController: exchange)
        
        exchangeAppVc.tabBarItem.title =  LanguageHelper.getString(key: "TabExchange")
        exchangeAppVc.tabBarItem.image = #imageLiteral(resourceName: "icon_exchange")
        exchangeAppVc.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_exchange_sel")
        exchangeAppVc.hidesBottomBarWhenPushed = false
        
        
        let location = LocationViewController()
        location.hidesBottomBarWhenPushed = false
        let locationAppVc = AppNavViewController(rootViewController: location)
        
        locationAppVc.tabBarItem.title =  LanguageHelper.getString(key: "TabLocation")
        locationAppVc.tabBarItem.image = #imageLiteral(resourceName: "icon_location")
        locationAppVc.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_location_sel")
        locationAppVc.hidesBottomBarWhenPushed = false
        
        
        let promotion = NoticeViewController()
        promotion.showHeader = true
         promotion.view.backgroundColor = UIColor.white
         promotion.hidesBottomBarWhenPushed = false
        let promotionAppVc = AppNavViewController(rootViewController: promotion)
        
        promotionAppVc.tabBarItem.title =  "世汇公告".localized()
        promotionAppVc.tabBarItem.image = #imageLiteral(resourceName: "icon_promotion")
        promotionAppVc.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_promotion_sel")
        promotionAppVc.hidesBottomBarWhenPushed = false
        
        
        let userVc = UserViewController()
        userVc.view.backgroundColor = UIColor.white
        userVc.hidesBottomBarWhenPushed = false
        let userAppVc = AppNavViewController(rootViewController: userVc)
        
        userAppVc.tabBarItem.title =  LanguageHelper.getString(key: "TabUserCenter")
        userAppVc.tabBarItem.image = #imageLiteral(resourceName: "icon_user")
        userAppVc.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_user_sel")
        userAppVc.hidesBottomBarWhenPushed = false
        
        self.viewControllers = [homeAppVc,exchangeAppVc,locationAppVc,promotionAppVc,userAppVc]
        
        self.loadCall()
    }


    func loadCall(){
        provider.rx.request(WE.call()).mapString().subscribe(onSuccess: { (result) in
          
           self.callItem =  ArticleItem.deserialize(from: result)
            
        }) { (error) in
            print(error)
        }
    }
    
    
    func showChangeShop() {
        let dialogViewController =  QMUIDialogSelectionViewController()
        dialogViewController.title = LanguageHelper.getString(key: "changeShopLabel")
        let shopNames = appShopItems.map { (item) -> String in
            return item.title
        }
        
        dialogViewController.items = shopNames
        
        dialogViewController.cellForItemBlock = { aDialogViewController, cell, itemIndex in
            cell?.accessoryType = UITableViewCell.AccessoryType.none // 移除点击时默认加上右边的checkbox
        }
        dialogViewController.heightForItemBlock = { aDialogViewController, itemIndex in
            return 54 // 修改默认的行高，默认为 TableViewCellNormalHeight
        }
        dialogViewController.didSelectItemBlock = { aDialogViewController, itemIndex in
            aDialogViewController?.hide()
            currentShop = appShopItems[Int(itemIndex)]
            NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: changeShopNoticeName), object: nil)
        }
        dialogViewController.show()
        
    }
    
    
    @objc func showConvert(){
        let convertVc = ConvertViewController()
        let modalVc = QMUIModalPresentationViewController()
        modalVc.contentViewController = convertVc
        modalVc.addChild(convertVc)
       
        self.present(modalVc, animated: true) {
            
        }
        
//       modalVc.showWith(animated: true, completion: nil)
    }
    
    @objc func telClick(){
//        let alert =  QMUIAlertController(title: LanguageHelper.getString(key: "CallText"), message: "+86 187366515635", preferredStyle: QMUIAlertControllerStyle.alert)
//        alert.addAction(QMUIAlertAction(title: LanguageHelper.getString(key: "BtnCall"), style: QMUIAlertActionStyle.destructive, handler: { (_, _) in
//
//        }))
//        alert.addAction(QMUIAlertAction(title: LanguageHelper.getString(key: "BtnCancel"), style: QMUIAlertActionStyle.cancel, handler: { (_, _) in
//
//        }))
//        alert.showWith(animated: true)
        
        let dialogViewController = QMUIDialogViewController()
        dialogViewController.title = LanguageHelper.getString(key: "CallText")
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
        contentView.backgroundColor = UIColor.white
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = self.callItem?.title
        label.sizeToFit()
        label.center = CGPoint(x: contentView.bounds.width / 2.0, y: contentView.bounds.height / 2.0)
        contentView.addSubview(label)
        dialogViewController.contentView = contentView
        dialogViewController.addCancelButton(withText: LanguageHelper.getString(key: "BtnCancel"), block: nil)
        dialogViewController.addSubmitButton(withText: LanguageHelper.getString(key: "BtnCall"), block: { aDialogViewController in
            aDialogViewController?.hide()
            if let tel = self.callItem?.title {
               var call = tel.replacingOccurrences(of: " ", with: "")
                let url = URL(string: "telprompt:\(call)")
                if UIApplication.shared.canOpenURL(url!){
                    UIApplication.shared.open(url!)
                }
            }
            
            
        })
        dialogViewController.show()
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
